﻿package 
{
    public class Controller
    {
        private var m_player:Player;
        private var m_maze:Maze;

        public function Controller(player:Player, maze:Maze)
        {
            m_player = player;

            m_maze = maze;

            m_player.addEventListener(PlayerEvent.PLAYER_MOVES, onPlayerMoves);
        }

        private function onPlayerMoves(e:PlayerEvent):void
        {
            var pX:Number = e.playerX;
            var pY:Number = e.playerY;

            var temp1:Number = (GameConstants.WIDTH - (GameConstants.DOOR_WIDTH / 2)) / 2;
            var temp2:Number = (GameConstants.WIDTH + (GameConstants.DOOR_WIDTH / 2)) / 2;
            if (pY - GameConstants.PLAYER_RADIUS < GameConstants.WALL_THICKNESS &&
                    pX > temp1 && pX < temp2 && m_maze.getCurrRoom().isWallWithDoor(GameConstants.NORTH))
            {
                m_maze.getCurrRoom().getWallWithDoor(GameConstants.NORTH).enter(m_player, m_maze);
            }
            if ((pY + GameConstants.PLAYER_RADIUS > GameConstants.HEIGHT - GameConstants.WALL_THICKNESS) &&
                    (pX > temp1) && (pX < temp2) && m_maze.getCurrRoom().isWallWithDoor(GameConstants.SOUTH))
            {
                m_maze.getCurrRoom().getWallWithDoor(GameConstants.SOUTH).enter(m_player, m_maze);
            }

            temp1 = (GameConstants.HEIGHT - (GameConstants.DOOR_WIDTH / 2)) / 2;
            temp2 = (GameConstants.HEIGHT + (GameConstants.DOOR_WIDTH / 2)) / 2;
            if ((pX + GameConstants.PLAYER_RADIUS > GameConstants.WIDTH - GameConstants.WALL_THICKNESS) &&
                    (pY > temp1) && (pY < temp2) && m_maze.getCurrRoom().isWallWithDoor(GameConstants.EAST))
            {
                m_maze.getCurrRoom().getWallWithDoor(GameConstants.EAST).enter(m_player, m_maze);
            }
            if ((pX - GameConstants.PLAYER_RADIUS < GameConstants.WALL_THICKNESS) &&
                    (pY > temp1) && (pY < temp2) && m_maze.getCurrRoom().isWallWithDoor(GameConstants.WEST))
            {
                m_maze.getCurrRoom().getWallWithDoor(GameConstants.WEST).enter(m_player, m_maze);
            }
        }
    }
}
